package com.sec.android.app.myfiles.facade.cmd;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.dialog.AbsDialogFragment;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.MimeType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.operation.ConvertAsyncTask;
import com.sec.android.app.myfiles.provider.PrivateMyFilesProvider;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.StorageMonitor;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.util.ArrayList;

public class ConvertCmd extends SimpleCommand implements ConvertAsyncTask.OnConvertTaskListener {

    private static final int MIN_FREE_SPACE = 10; // 10MB

    private AbsMyFilesFragment mFragment;
    private Activity mActivity;
    private FileRecord mRecord;
    private ConvertAsyncTask mConvertTask;
    private ConvertDialogFragment mDialogFragment;

    public static class ConvertDialogFragment extends AbsDialogFragment {
        AbsMyFilesFragment mFragment;
        Activity mActivity;
        FileRecord mRecord;
        ConvertCmd mConvertCmd;

        public static ConvertDialogFragment createInstance(AbsMyFilesFragment fragment, FileRecord record, ConvertCmd convertCmd) {
            ConvertDialogFragment cd = new ConvertDialogFragment();
            cd.initValue(fragment, record, convertCmd);
            return cd;
        }

        private void initValue(AbsMyFilesFragment fragment, FileRecord record, ConvertCmd convertCmd) {
            mFragment = fragment;
            mActivity = mFragment.getActivity();
            mRecord = record;
            mConvertCmd = convertCmd;
        }

        @Override
        protected Dialog _createDialog() {
            ArrayList<String> listItems = new ArrayList<>();
            int selectedCnt = mFragment.getSelectedFileCount();
            if (selectedCnt > 1) {
                listItems.add(mFragment.getString(R.string.image_files));
                listItems.add(mFragment.getString(R.string.video_files));

            } else {
                listItems.add(mFragment.getString(R.string.image_file));
                listItems.add(mFragment.getString(R.string.video_file));
            }
            CharSequence[] items = listItems.toArray(new CharSequence[listItems.size()]);

            AlertDialog dialog = new AlertDialog.Builder(mActivity).setTitle(R.string.share_as).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case AppConstants.ConvertType.AS_VIDEO_FILES:
                            if (StorageMonitor.getStorageFreeSpace(mActivity, StorageMonitor.getMatchedStorageType(mRecord.getFullPath())) >= MIN_FREE_SPACE) {
                                mConvertCmd.convert(mRecord, AppConstants.ConvertType.AS_VIDEO_FILES);
                            } else {
                                Toast.makeText(mActivity, R.string.not_enough_memory_popup, Toast.LENGTH_LONG).show();
                            }
                            break;
                        case AppConstants.ConvertType.AS_IMAGE_FILES:
                            if ((MimeType.IMAGE_GOLF).equalsIgnoreCase(mRecord.getMimeType(mFragment.context()))) {
                                mConvertCmd.convert(mRecord, AppConstants.ConvertType.AS_IMAGE_FILES);
                            } else {
                                MyFilesFacade.shareVia(mFragment.getProcessId(), mFragment.context(), mFragment, mConvertCmd.makeFileRecordList(mRecord), true);
                            }
                            break;
                    }
                    dialog.dismiss();
                }
            }).create();
            return dialog;
        }
    }

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        mFragment = (AbsMyFilesFragment) params[0];
        mActivity = mFragment.getActivity();
        mRecord = (FileRecord) params[1];
        showConvertTypeSelectDialog();
    }

    private void showConvertTypeSelectDialog() {
        mDialogFragment = ConvertDialogFragment.createInstance(mFragment, mRecord, this);
        mDialogFragment.show(mFragment.getFragmentManager(), "convert_dialog");
    }

    private void convert(FileRecord record, int convertType) {
        executeConvertAsyncTask(record, convertType);
        MyFilesFacade.execSpinnerProgress(mFragment.mProcessId, mFragment.getContext(), SpinnerProgressCmd.OperationProgress.SHOW);
    }

    private void executeConvertAsyncTask(FileRecord record, int convertType) {
        Log.d(this, "executeFileAsyncTask is called");

        ConvertAsyncTask.ConvertInfo info = new ConvertAsyncTask.ConvertInfo();

        info.path = record.getFullPath();
        info.mimeType = record.getMimeType(mFragment.context());
        info.convertType = convertType;

        mConvertTask = new ConvertAsyncTask();
        mConvertTask.setOnTaskListener(ConvertCmd.this);
        mConvertTask.execute(info, null, null);
    }

    @Override
    public void onTaskComplete(String result) {
        Log.d(this, "onTaskComplete is called");

        if (result != null) {
            FileUtils.sendScanSingleFile(mFragment.context(), result);
            MyFilesFacade.shareVia(mFragment.getProcessId(), mFragment.context(), mFragment, makeFileRecordList(result), true);
        } else {
            Toast.makeText(mActivity, mFragment.getResources().getString(R.string.send_failed), Toast.LENGTH_SHORT).show();
        }

        PrivateMyFilesProvider.refreshDb(mFragment.context(), mRecord.getParent(), true);
        MyFilesFacade.execSpinnerProgress(mFragment.mProcessId, mFragment.getContext(), SpinnerProgressCmd.OperationProgress.DISMISS);
        mConvertTask = null;
    }

    private ArrayList<FileRecord> makeFileRecordList(FileRecord record) {
        ArrayList<FileRecord> ret = new ArrayList<>();
        ret.add(record);

        return ret;
    }

    private ArrayList<FileRecord> makeFileRecordList(String path) {
        FileRecord convertedFileRecord = FileRecord.createFileRecord(FileRecord.StorageType.Local, path);
        return makeFileRecordList(convertedFileRecord);
    }
}
